package com.cictec.middleware.gps.engine.position.handler.operateState;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cictec.middleware.commons.utils.Constants;
import com.cictec.middleware.commons.utils.DateUtils;
import com.cictec.middleware.core.entity.DeparterSchedule;
import com.cictec.middleware.gps.engine.BusInstance;
import com.cictec.middleware.gps.engine.exception.BusPositionHandlerException;
import com.cictec.middleware.gps.engine.position.handler.BusPositionOperationHandler;
import com.cictec.middleware.gps.service.ScheduleService;

/**
 * 运营状态处理-【运营时间】影响的运营状态
 * @author ql
 * @date 2017年9月18日
 */
@Service
public class OperateTimeHandler implements OperationStateHandler{

	private static Logger logger = LoggerFactory.getLogger(BusPositionOperationHandler.class);
	
	@Autowired
	private ScheduleService scheduleService;
	
	@Override
	public void onHandler(BusInstance busInstance) throws BusPositionHandlerException {
		
		/* ************** 是否在运营时间范围内判定 start ****************************/
		DeparterSchedule departerSchedule = scheduleService.getDeparterScheduleFirst(busInstance.getTime(), busInstance.getLineUuid(), busInstance.getBusUuid());
		if(departerSchedule != null){
			boolean inOper = false;
			
			String inOperTime = DateUtils.formatDateTimes(departerSchedule.getDepartureTime());
			String dataTIme = DateUtils.formatDateTimes(busInstance.getTime());
			
			if(busInstance.getTime().getTime() >= departerSchedule.getDepartureTime().getTime() - 15 * 60 * 1000){
				inOper = true;
			}
			
			if (inOper) {
				logger.debug("\t{} 【运营时间状态】在运营时间范围内", busInstance.getBusNumber());
			} else {
				logger.debug("\t{} 【运营时间状态】不在运营时间范围内 , 运营开始时间：{}, 数据上传时间:{}", busInstance.getBusNumber(), inOperTime, dataTIme);
			}
			
			busInstance.setAttributeInOperateTime(inOper?Constants.VAL_BUS_OPER_STATUS_1:Constants.VAL_BUS_OPER_STATUS_0);
		}else{
			logger.debug("\t{} 【运营时间状态】没有运营时间限制，默认运营状态", busInstance.getBusNumber());
			busInstance.setAttributeInOperateTime(Constants.VAL_BUS_OPER_STATUS_1);
		}
//		busInstance.setStatusInOperateTime(Constants.VAL_BUS_OPER_STATUS_1);	// 默认运营
		/* ************** 是否在运营时间范围内判定 end ****************************/
	}

}
